<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Examples</title>
<meta name="description" content="">
<meta name="keywords" content="">
<link href="" rel="stylesheet">
</head>
<body>
    
    <form method="get" action="{:url('test')}">
    	<input type="text" name="n">
    	<button>提交</button>
    </form>
    <button class="ajax">ajax</button>
    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script type="text/javascript" src="{:DOCUMENT_ROOT}public/static/base64.js"></script>
    <script type="text/javascript" src="{:DOCUMENT_ROOT}public/static/md5.js"></script>
    
    <script type="text/javascript">
        (function($){  
            //首先备份下jquery的ajax方法  
            var _ajax=$.ajax;  
               
            //重写jquery的ajax方法  
            $.ajax=function(opt){  
                //备份opt中error和success方法  
                var fn = {  
                    error:function(XMLHttpRequest, textStatus, errorThrown){},  
                    success:function(data, textStatus){}  ,
                    beforeSend:function(XHR, o){}  ,
                    complete:function(XHR, TS){}  ,
                }  
                if(opt.error){  
                    fn.error=opt.error;  
                }  
                if(opt.success){  
                    fn.success=opt.success;  
                }
                if(opt.beforeSend){  
                    fn.beforeSend=opt.beforeSend;  
                }
                if(opt.complete){  
                    fn.complete=opt.complete;  
                } 
                   
                //扩展增强处理  
                var _opt = $.extend(opt,{  
                    error:function(XMLHttpRequest, textStatus, errorThrown){  
                        //错误方法增强处理  
                        fn.error(XMLHttpRequest, textStatus, errorThrown);  
                    },  
                    success:function(data, textStatus){  
                        //成功回调方法增强处理  
                        fn.success(data, textStatus);  
                    },  
                    beforeSend:function(XHR,o){  
                        //提交前回调方法  
                        var token=window.top.token;
                        var signature=signatureEncode(o.url,token)
                        console.log(signatureDecode(signature))
                        fn.beforeSend(XHR,o);
                        XHR.setRequestHeader('user-signature',signature);
                        XHR.setRequestHeader('user-token',token);
                    },  
                    complete:function(XHR, TS){  
                        //请求完成后回调函数 (请求成功或失败之后均调用)。  
                        fn.complete(XHR, TS);
                    }  
                });  
                return _ajax(_opt);  
            };  
        })(jQuery);  
    </script>
    <script type="text/javascript">
        var token="{$token}";

        function signatureEncode(str,key) {
            var t = "", c=0, k=md5(key), s=Base64.encode(str);
            for (var i = 0; i < s.length; i++) {
                c=c==k.length?0:c;
                t+=s[i]+k[c++];
            }
            return Base64.encode(t);
        }

        function signatureDecode(str){
            var t = "",s=Base64.decode(str);
            for(var i=0; i < s.length; i++){
               t+=s[i++];
            }
            return Base64.decode(t);
        }


    	$('form').submit(function(e){
    		alert($(this).attr('action'))
    		console.log(e);
    	})
        $('.ajax').click(function(){
            // $.post("{:url('test')}",{n:'5566'},function(d){
            //     console.log(d);
            // })
            $.ajax({
                url:"{:url('test')}",
                data:{n:'ddd'},
                type:'get',
                beforeSend:function(e){ },
                success:function(d){ }
            })
        });
 
    </script>
</body>
</html>